﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Server.Models;
using Server.InvoiceService;
using Box51;
using MessageModel;


namespace Server.Invoice
{
    public class InvoiceBLL
    {
        BoxProtocol box = new BoxProtocol();
        InvoiceDAL iDal = new InvoiceDAL();
        /// <summary>
        /// 3.1税控信息查询
        /// </summary>
        /// <param name="sk"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public SKQueryDataModel GetTaxInfo(SKQueryRequestModel sk, UserInfo user)
        {
            try
            {
                //获取设备信息
                DeviceInfoModel dm = this.GetDevice(user.DeviceId);
                //操作盒子
                box.ServerAddress = dm.DeviceIP;
                SKQueryOutputModel skOut = new SKQueryOutputModel();
                skOut = box.GetTaxInfo(sk, user.TaxNum, user.DeviceSession);
                if (skOut.ReturnData == null)
                    throw new Exception("解析错误!");
                return skOut.ReturnData;

            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 3.2发票库存查询
        /// </summary>
        public TaxCardInvoiceStockQueryReturnModel GetTaxKC(TaxCardInvoiceStockQueryModel TaxKC, UserInfo user)
        {
            try
            {
                //获取设备信息
                DeviceInfoModel dm = this.GetDevice(user.DeviceId);
                //操作盒子
                box.ServerAddress = dm.DeviceIP;
                TaxCardInvoiceStockQueryReturnModel skOut = new TaxCardInvoiceStockQueryReturnModel();
                skOut = box.GetTaxKC(TaxKC, user.TaxNum, user.DeviceSession);
                return skOut;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 3.3发票开具
        /// </summary>
        /// <param name="Inv"></param>
        /// <param name="user"></param>
        public InvoiceReturnModel InvoiceInfo(InvoiceIssueModel Inv, UserInfo user)
        {
            try
            {
                //获取设备信息
                DeviceInfoModel dm = this.GetDevice(user.DeviceId);
                //操作盒子
                box.ServerAddress = dm.DeviceIP;
                InvoiceReturnModel skOut = new InvoiceReturnModel();
                skOut = box.InvoiceInfo(Inv, user.TaxNum, user.DeviceSession);
                return skOut;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 3.4发票查询
        /// </summary>
        public InvoiceQueryReturn GetFP(InvoiceQueryModel Inv, UserInfo user)
        {
            try
            {
                //获取设备信息
                DeviceInfoModel dm = this.GetDevice(user.DeviceId);
                //操作盒子
                box.ServerAddress = dm.DeviceIP;
                InvoiceQueryReturn invRet = new InvoiceQueryReturn();
                invRet = box.GetFP(Inv, user.TaxNum, user.DeviceSession);
                return invRet;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 3.5下载税收分类编码(开具发票时获取税收分类编码)
        /// </summary>
        public TaxCodingReturn DownLoadTaxCode(TaxCodingMessageModel tax, UserInfo user)
        {
            try
            {
                //获取设备信息
                DeviceInfoModel dm = this.GetDevice(user.DeviceId);
                //操作盒子
                box.ServerAddress = dm.DeviceIP;
                TaxCodingReturn taxret = box.TaxCode(tax, user.TaxNum, user.DeviceSession);
                return taxret;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 获取设备信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DeviceInfoModel GetDevice(int id)
        {
            try
            {
                var dt = iDal.GetDevice(id);
                var dr = dt.Rows[0];
                DeviceInfoModel dm = new DeviceInfoModel()
                {
                    id = Convert.ToInt32(dr["id"]),
                    DeviceSession = dr["DeviceSession"].ToString(),
                    SessionExpired = Convert.ToDateTime(dr["SessionExpired"]),
                    DeviceIP = dr["DeviceIP"].ToString(),
                    Number = Convert.ToInt32(dr["Number"]),
                    Remark = dr["Remark"].ToString()
                };
                return dm;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
